class Book(val bookName:String,val author:String,val ISBN:String){
  def lend():Unit={
    println("已经借出")
  }
  def back():Unit={
    println("已经归还")
  }
}
object Book{
  def apply(bookName:String,author:String,ISBN:String)=new Book(bookName,author,ISBN)
  def unapply(book:Book):Option[(String,String,String)]={
    Some(book.bookName,book.author,book.ISBN)
   }
}

class Library(var usableList:List[Book]=Nil,var lendedList:List[Book]=Nil){
  def addBook(name:Book):Unit={
    usableList=usableList :+ name
  }
  def lendBook(ISBN:String){
    val r=findUsableBookByISBN(ISBN)
    if (usableList.length>0 && r>=0){
      val book=usableList(r)
      book.lend()
      lendedList=lendedList :+ book
      usableList=usableList.filter(_!=book)
    }else{
      println("这本书不存在！")
    }
  }
  def backBook(ISBN:String){
    val r=findLendedBookByISBN(ISBN)
    if (usableList.length>0 && r>=0){
      val book=usableList(r)
      book.back()
      lendedList=lendedList.filter(_!=book)
      usableList=usableList :+ book
    }else{
      println("这本书没有找到借出记录！")
    }
  }
  def findUsableBookByISBN(ISBN:String):Int={
    var r=-1
    //val len = usableList.length
    if (usableList.length != 0){
      for(i <- 1 to usableList.length){
        if (usableList(i-1).ISBN == ISBN){
          r=i
        }
      }
    }
  }
  def findLendedBookByISBN(ISBN:String):Int={
    var r=-1
    //val len = lendedList.length
    if (lendedList.length != 0){
      for(i <- 1 to lendedList){
        if (lendedList(i-1).ISBN == ISBN){
          r=i
        }
      }
    }
  }
}
